package com.pirestupppai.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.pirestupppai.R;
import com.pirestupppai.bean.SelectPicture;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 *  多图选择适配
 *
 * @author: xiaoluo
 * @date: 2017-01-16 17:11
 */
public class SelectPictureAdapter extends BaseAdapter {
    private final static String TAG = SelectPictureAdapter.class.getSimpleName();

    private static final int TYPE_CAMERA = 0;
    private static final int TYPE_NORMAL = 1;

    private boolean hasCamera = true;

    private Context mContext;
    private LayoutInflater mInflater;
    private List<SelectPicture> mImages = new ArrayList<SelectPicture>();
    private List<SelectPicture> mSelectedImages = new ArrayList<SelectPicture>();

    private int mItemSize;
    private GridView.LayoutParams mItemLayoutParams;

    public SelectPictureAdapter(Context context) {
        mContext = context;
        mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mItemLayoutParams = new GridView.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    }

    /**
     * 选择某个图片，改变选择状态
     *
     * @param image
     */
    public void select(SelectPicture image) {
        if (mSelectedImages.contains(image)) {
            mSelectedImages.remove(image);
        } else {
            mSelectedImages.add(image);
        }
        notifyDataSetChanged();
    }

    /**
     * 通过图片路径设置默认选择
     *
     * @param resultList
     */
    public void setDefaultSelected(List<String> resultList) {
        mSelectedImages.clear();
        for (String path : resultList) {
            SelectPicture image = new SelectPicture(path, "", 0);
//            if (image != null) {
            mSelectedImages.add(image);
//            }
        }
        notifyDataSetChanged();
    }

    public SelectPicture getImageByPath(String path) {
        if (mImages != null && mImages.size() > 0) {
            for (SelectPicture image : mImages) {
                if (image.path.equalsIgnoreCase(path)) {
                    return image;
                }
            }
        }
        return null;
    }

    /**
     * 设置数据集
     *
     * @param images
     */
    public void setData(List<SelectPicture> images) {
        mSelectedImages.clear();

        if (images != null) {
            mImages = images;
        } else {
            mImages.clear();
        }
        notifyDataSetChanged();
    }

    /**
     * 重置每个Column的Size
     *
     * @param columnWidth
     */
    public void setItemSize(int columnWidth) {

        if (mItemSize == columnWidth) {
            return;
        }

        mItemSize = columnWidth;

        mItemLayoutParams = new GridView.LayoutParams(mItemSize, mItemSize);

        notifyDataSetChanged();
    }


    @Override
    public int getViewTypeCount() {
        return hasCamera ? 2 : 1;
    }

    @Override
    public int getItemViewType(int position) {
        return hasCamera ? position == 0 ? TYPE_CAMERA : TYPE_NORMAL : TYPE_NORMAL;
    }

    @Override
    public int getCount() {
        return mImages.size() + (hasCamera ? 1 : 0);
    }

    @Override
    public SelectPicture getItem(int position) {
        if (hasCamera) {
            if (position == 0) {
                return null;
            } else {
                return mImages.get(position - 1);
            }
        } else {
            return mImages.get(position);
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        int type = getItemViewType(position);
        if (type == TYPE_CAMERA) {
            view = mInflater
                    .inflate(R.layout.item_select_camera, parent, false);
        } else if (type == TYPE_NORMAL) {
            ViewHolder holde;
            if (view == null) {
                view = mInflater.inflate(R.layout.item_select_image, parent,
                        false);
                holde = new ViewHolder(view);
            } else {
                holde = (ViewHolder) view.getTag();
                if (holde == null) {
                    view = mInflater.inflate(R.layout.item_select_image,
                            parent, false);
                    holde = new ViewHolder(view);
                }
            }
            if (holde != null) {
                holde.bindData(getItem(position));
            }
        }

        /** Fixed View Size */
        GridView.LayoutParams lp = (GridView.LayoutParams) view
                .getLayoutParams();
        if (lp.height != mItemSize) {
            view.setLayoutParams(mItemLayoutParams);
        }

        return view;
    }

    class ViewHolder {
        ImageView image;
        ImageView indicator;
        ImageView cover;

        ViewHolder(View view) {
            image = (ImageView) view.findViewById(R.id.image);
            indicator = (ImageView) view.findViewById(R.id.checkmark);
            cover = (ImageView) view.findViewById(R.id.image_cover);
            view.setTag(this);
        }

        void bindData(final SelectPicture data) {
            if (data == null)
                return;
            // 处理单选和多选状态
            if (mSelectedImages.contains(data)) {
                // 设置选中状态
                image.setBackgroundResource(R.drawable.shape_select_image_selected);
                indicator.setImageResource(R.drawable.ic_selected);

                if (cover.getVisibility() == View.GONE) {
                    cover.setVisibility(View.VISIBLE);
                }
            } else {
                // 未选择
                image.setBackgroundResource(R.drawable.shape_select_image_unselect);
                indicator.setImageResource(R.drawable.ic_normal);
                if (cover.getVisibility() == View.VISIBLE) {
                    cover.setVisibility(View.GONE);
                }
            }
            File imageFile = new File(data.path);
//			Bitmap bitmap = BitmapFactory.decodeFile()
            if (mItemSize > 0) {
                // 显示图片
                Glide.with(mContext)
                        .load(imageFile)
                        .placeholder(R.drawable.default_error)
                        .override(mItemSize, mItemSize)
                        .centerCrop()
                        .into(image);
            }
        }
    }

    public void setHasCamera(boolean hasCamera) {
        this.hasCamera = hasCamera;
    }
}